import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.Kernel;
import java.text.DecimalFormat;

public class Demo_3 {


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                caculateGUI();
            }
        });
    }
    private static void caculateGUI(){
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame();
        frame.setTitle("caculator");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("caculator");
        frame.setLayout(new GridLayout(5,2));

        //add menubar
        JMenuBar jMenuBar = new JMenuBar();
        JMenu operationMenu = new JMenu("operation");
        JMenu helpMenu = new JMenu("help");
        jMenuBar.add(operationMenu);
        jMenuBar.add(helpMenu);

        //add exit actionlistener
        JMenuItem operationExitMI = new JMenuItem("Exit");
        operationMenu.add(operationExitMI);
        operationExitMI.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });


        //set menubar
        frame.setJMenuBar(jMenuBar);
        frame.setSize(50, 60);
        frame.setVisible(true);


        //add label
        frame.add(new JLabel("1st num"));
        frame.add(new JLabel("2nd num"));

        //creat and add textfield
        JTextField textField1 = new JTextField();
        JTextField textField2 = new JTextField();
        frame.add(textField1);
        frame.add(textField2);


        //add result_label and result_textarea
        frame.add(new JLabel("result"));
        JTextArea jTextArea = new JTextArea();
        frame.add(jTextArea);


        //creat buttons
        JButton jButton1 = new JButton("+");
        JButton jButton2 = new JButton("-");
        JButton jButton3 = new JButton("*");
        JButton jButton4 = new JButton("/");
        JButton jButton5 = new JButton("^");
        JButton jButton6 = new JButton("cos");
        JButton jButton7 = new JButton("sin");


        //buttonActions
        frame.add(jButton1);
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int a = Integer.parseInt(textField1.getText());
                int b = Integer.parseInt(textField2.getText());
                jTextArea.setText(Integer.toString(a+b));
            }
        });

        frame.add(jButton2);
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int a = Integer.parseInt(textField1.getText());
                int b = Integer.parseInt(textField2.getText());
                jTextArea.setText(Integer.toString(a-b));
            }
        });

        frame.add(jButton3);
        jButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int a = Integer.parseInt(textField1.getText());
                int b = Integer.parseInt(textField2.getText());
                jTextArea.setText(Integer.toString(a*b));
            }
        });

        frame.add(jButton4);
        jButton4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                double a = Double.parseDouble(textField1.getText());
                double b = Double.parseDouble(textField2.getText());
                jTextArea.setText(Double.toString(a/b));
            }
        });
        frame.add(jButton5);
        jButton5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int a = Integer.parseInt(textField1.getText());
                int b = Integer.parseInt(textField2.getText());
                jTextArea.setText(Integer.toString(a^b));
            }
        });
        frame.add(jButton6);
        jButton6.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                double a = Integer.parseInt(textField1.getText());
                a=Math.toRadians(a);
                System.out.println((Math.sin(a)));
            }
        });
        frame.add(jButton7);
        jButton7.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                double a = Integer.parseInt(textField1.getText());
                a=Math.toRadians(a);
                System.out.println((Math.cos(a)));
            }
        });

        int frameWidth = 200;
        int frameHeight = 400;
        Dimension screenSize =
                Toolkit.getDefaultToolkit().getScreenSize();
        frame.setBounds((int)screenSize.getWidth() - frameWidth,0,frameWidth,frameHeight);
        frame.setVisible(true);

    }

}

